package com.dictionary.controller;

import com.dictionary.common.Result;
import com.dictionary.pojo.Word;
import com.dictionary.service.WordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Classname WordController
 * @Description TODO
 * @Date 2025/5/15 21:04
 * @Created by xxx
 */
@Slf4j
@RestController
@RequestMapping("/word")
public class WordController {

    @Autowired
    private WordService wordService;

    @GetMapping("/list")
    public Result<List<Word>> getWordList() {
        log.info("获取单词列表");
        try {
            List<Word> wordList = wordService.getWordList();
            return Result.success(wordList);
        } catch (Exception e) {
            log.error("获取单词列表失败: {}", e.getMessage(), e);
            return Result.error("获取单词列表失败");
        }
    }

    @GetMapping
    public Result<Word> getWordDetail(@RequestParam String word) {
        log.info("获取单词详情: {}", word);
        try {
            Word wordDetail = wordService.getWordDetail(word);
            if (wordDetail == null) {
                return Result.error("单词不存在");
            }
            return Result.success(wordDetail);
        } catch (Exception e) {
            log.error("获取单词详情失败: {}", e.getMessage(), e);
            return Result.error("获取单词详情失败");
        }
    }
}
